<template>
  <el-dialog
    title="审核"
    :visible.sync="visible"
    width="60%"
    append-to-body
  >
    <avue-form
      ref="addForm"
      v-model="form"
      :option="tableOption"
    ></avue-form>
    <div class="flex-end">
      <el-button @click="visible = false">取消</el-button>
      <el-button
        class="btn"
        type="primary"
        :loading="loading"
        @click="confirm"
      >
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { mapGetters } from 'vuex';
import { getObj, putObj } from '@/api/core/senderApply';
import { tableOption } from '@/const/crud/customer/senderApplyAudit';
export default {
  name: 'auditPopup',
  props: {
    id: {
      type: [Object, String],
    },
    value: {
      type: Boolean,
      default: false,
    },
    dataObj: { type: [Object] },
  },
  data() {
    return {
      form: {},
      loading: false,
      tableOption: tableOption(),
    };
  },
  computed: {
    visible: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit('input', val);
      },
    },
    ...mapGetters(['userInfo', 'access_token']),
  },
  watch: {
    visible(val) {
      if (val) {
        this.id && this.getDetail();
      } else {
        this.$refs.addForm && this.$refs.addForm.resetForm();
      }
    },
  },
  methods: {
    confirm() {
      this.$refs.addForm.validate(async (valid, done) => {
        if (valid) {
          this.loading = true;
          try {
            const params = { ...this.form, id: this.id };
            await putObj(params);
            this.$message.success('操作成功');
            this.$emit('success', true);
            done();
            this.loading = false;
            this.visible = false;
          } catch (error) {
            done();
            this.loading = false;
            throw new Error(error);
          }
        }
      });
    },
    async getDetail() {
      const res = await getObj(this.id);
      this.form = res.data.data;
      this.form.name = res.data.data.name;
      this.form.nickName = res.data.data.nickName;
      this.form.code = res.data.data.code;
      this.form.age = res.data.data.age;
      this.form.sex = res.data.data.sex;
      this.form.phone = res.data.data.phone;
      this.form.address = res.data.data.address;
      this.form.applyTime = res.data.data.applyTime;
    },
  },
};
</script>

<style lang="scss" scoped></style>
